﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class HoaDonDAO
    {
        ConnectDB conn = new ConnectDB();
        string str;
        //Load dữ liệu
        public DataTable Load_HoaDon()
        {
            return conn.LayDuLieu("select *from HoaDon");
        }
        //lay san pham
        public DataTable SP()
        {
            return conn.LayDuLieu("select MaSP, GiaBan from SanPham ");
        }
        //lay khach hang
        public DataTable KH()
        {
            return conn.LayDuLieu("select MaKH from KhachHang");
        }
        //Lay nhan vien
        public DataTable NV()
        {
            return conn.LayDuLieu("select MaNV from NhanVien");
        }
        // Insert và Update
        public void IsUp_HoaDon(HoaDonDTO p, Boolean flag)
        {
            string MaHD = p.MaHD;
            string MaSP = p.MaSP;
            string MaKH = p.MaKH;
            float SoLuong = p.SoLuong;
            float TongTien = p.TongTien;
            DateTime NgayMua = p.NgayMua;
            string MaNV = p.MaNV;
            float GiaBan = p.GiaBan;
            if(flag==true )
            {//(MaHD, MaSP, MaKH, MaKH, SoLuong, TongTien, NgayMua, MaNV, GiaBan)
                str = "INSERT HoaDon (MaSP, MaKH, SoLuong, TongTien, NgayMua, MaNV, GiaBan) VALUES('" + MaSP + "','" + MaKH + "'," + SoLuong + "," + TongTien + ",'" + NgayMua + "','" + MaNV + "'," + GiaBan + ")";
            }
            else if(flag ==false)
            {
                str ="UPDATE HoaDon SET MaSP ='"+ MaSP +"', MaKH ='"+ MaKH +"', SoLuong ="+ SoLuong + ", TongTien ="+ TongTien +", NgayMua ='"+ NgayMua +"', MaNV ='"+ MaNV +"', GiaBan="+ GiaBan +" WHERE MaHD='"+ MaHD +"'";
            }
            conn.excute(str);

        }
        //delete
        public void Del_HoaDon(HoaDonDTO p)
        {
            string MaHD = p.MaHD;
            str="DELETE HoaDon WHERE MaHD='"+ MaHD +"'";
            conn.excute(str);
        }
        //Tim kiem

        public DataTable TimHD(HoaDonDTO p, Boolean flag)
        {
            string MaHD = p.MaHD;
            string MaKH = p.MaKH;
            if (flag ==true)
            {
                str = "select * from HoaDon where MaHD like'%" + MaHD + "%'";
            }
            else
            {
                str = "select * from HoaDon where MaKH like'%" + MaKH + "%'";
            }
           return  conn.LayDuLieu(str);
            
        }
    }
}
